package com.wtzz.police.base.controller;

import com.wtzz.police.base.bean.dto.DepartmentDto;
import com.wtzz.police.base.bean.dto.DepartmentInfoDto;
import com.wtzz.police.base.model.enums.Role;
import com.wtzz.police.base.model.enums.WtzzResponse;
import com.wtzz.police.base.model.response.ResponseBean;
import com.wtzz.police.base.service.DepartmentService;
import com.wtzz.police.base.model.annotaions.RoleCheck;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 单位Controller
 */
@Api(tags = "单位")
@RestController
@RequestMapping("department")
@Validated
public class DepartmentController {

    @Autowired
    private DepartmentService service;

    @PostMapping
    @RoleCheck(Role.ADMIN)
    @ApiOperation(value = "新增部门(管理员权限)", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseBean addDepartment(
            @RequestBody
            @Validated
            @NotNull(message = "没提交数据")
            @ApiParam(value = "部门信息", type = "body", required = true)
                    DepartmentDto departmentDto) {
        service.add(departmentDto);
        return new ResponseBean<>(true, WtzzResponse.SUCCESS_OPTION);
    }

    @DeleteMapping("{id}")
    @RoleCheck(Role.ADMIN)
    @ApiOperation(value = "删除部门(管理员权限)")
    public ResponseBean deleteDepartment(
            @PathVariable
            @NotBlank(message = "部门Id不能为空")
            @ApiParam(value = "部门Id", type = "path", required = true)
                    String id) {
        service.logicalDelete(id);
        return new ResponseBean<>(true, WtzzResponse.SUCCESS_OPTION);
    }

    @PutMapping("{id}")
    @RoleCheck(Role.ADMIN)
    @ApiOperation(value = "修改部门(管理员权限)", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseBean updateDepartment(
            @PathVariable
            @NotBlank(message = "部门Id不能为空")
            @ApiParam(name = "id", value = "部门Id", type = "path", required = true)
                    String id,
            @RequestBody
            @Validated
            @NotNull(message = "没提交数据")
            @ApiParam(name = "departmentDto", value = "部门信息", type = "body", required = true)
                    DepartmentDto departmentDto) {
        service.update(id, departmentDto);
        return new ResponseBean<>(true, WtzzResponse.SUCCESS_OPTION);
    }

    @PostMapping("list")
    @ApiOperation(value = "查找全部部门", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseBean<List<DepartmentInfoDto>> listDepartment() {
        List<DepartmentInfoDto> list = service.listAllNotDeleted();
        return new ResponseBean<>(true, list);
    }

}
